<?php
/**
* 颁奖
*/
class Award extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->model('activity_lottery_model', 'activity_lottery');
        $this->load->model('lottery_winner_model', 'lottery_winner');
        $this->load->model('user_lottery_model', 'user_lottery');
    }

    /**
     * 选择哪个活动颁奖
     */
    public function index()
    {
        $result = $this->activity_lottery->findReseller($this->uid);
        if ($result->num_rows() == 0) {
            $this->load->view('home/award/nothing', $this->data);
        } elseif ($result->num_rows() == 1) {
            $this->redirect('home/award/info/'.$result->row()->lottery_id);
        } else {
            $this->data['lottery'] = $result->result();
            $this->load->view('home/award/index', $this->data);
        }
    }

    public function info($lotteryId)
    {
        if (!$lotteryId) {
            $this->alertJumpPre('活动不存在');
        }
        $result = $this->activity_lottery->findOne($lotteryId);
        if ($result->num_rows() == 0) {
            $this->alertJumpPre('活动不存在');
        }
        $this->data['lottery'] = $result->row();
        $this->load->view('home/award/info', $this->data);
        
    }

    /**
     * 异步加载 中奖总数  抽奖总数统计 
     */
    public function ajaxWinnersTotal($lotteryId)
    {
        $params = array('lottery_id'=> $lotteryId, 'reseller_name' => $this->userName);
        $data['winnersTotal'] = $this->lottery_winner->todayTotal($params);
        if ($data['winnersTotal']) {
            $data['todayWinnersTotal'] = $this->lottery_winner->todayTotal($params, TRUE);//查询今天
        }

        // $data['userLotteryTotal'] = $this->user_lottery->total($params);
        // if ($data['userLotteryTotal'] > 0) {
        //     $data['todayUserLotterytotal'] = $this->user_lottery->total($params, TRUE);//查询今天
        // }
        $data['lotteryId'] = $lotteryId;
        echo json_encode(array(
                'html' => $this->load->view('home/award/ajaxWinnersTotal', $data, TRUE)
            ));
        exit;
    }

    /**
     * ajax 异步刷新 中奖人数
     */
    public function ajaxWinnersList()
    {
        $data = array();
        $postData = $this->input->post();
        $postData['reseller_name'] = $this->userName;
        $num = 20;
        $pages = empty($postData['pages']) ? 0 : $postData['pages'];
        if ( !$postData['lottery_id']) {
            $this->jsonMessage('中奖信息有误');
        }
        $total = $this->lottery_winner->winersTotal($postData);
        $count = $pages * $num;
        if ($total > 0) {
            $data['winnersList'] = $this->lottery_winner->winnersList($postData, $count, $num);
        }

        echo json_encode(
            array(
                'status' => ($total == 0) ? FALSE : TRUE,
                'html'   => $this->load->view('home/award/winnersList', $data, TRUE),
                'more'   => ($total > ($count+$num)) ? TRUE : FALSE,
                'sql' => $this->db->last_query()
            )
        );
        exit;
    }

    /**
     * 确认颁奖
     */
    public function winnersPost()
    {
        $id = $this->input->post('id');
        if ( !$id) {
            $this->jsonMessage('中奖信息有误');
        }
        $result = $this->lottery_winner->findById($id);
        if ($result->num_rows() == 0) {
            $this->jsonMessage('中奖信息有误');
        }
        $result= $result->row();

        if ($result->status != 2) {
            $this->jsonMessage('已经确认过了');
        }

        $update = $this->lottery_winner->updateInfo($result->id, array('status' => 3));

        if ($update) {
             $this->jsonMessage('');
        } else {
            $this->jsonMessage('数据异常错误');
        }
    }

    /**
     * 抽奖用户
     */
    public function lotteryUser($lotteryId = FALSE)
    {
        if ( $lotteryId === FALSE || $lotteryId <= 0) {
            $this->alertJumpPre('抽奖活动不存在');
        }
        $this->data['lotteryId'] = $lotteryId;
        $this->data['total'] = $this->user_lottery->total($lotteryId);
        $this->load->view('home/award/lotteryUser', $this->data);
    }

    /**
     * ajax 异步刷新 参与用户
     */
    public function lotteryUserList()
    {
        $data = array();
        $postData = $this->input->post();
        $num = 20;
        $pages = empty($postData['pages']) ? 0 : $postData['pages'];
        if ( !isset($postData['lottery_id']) || $postData['lottery_id'] <= 0) {
            $this->jsonMessage('活动不存在');
        }
        $total = $this->user_lottery->userLotteryTotal($postData);
        $count = $pages * $num;
        if ($total > 0) {
            $data['list'] = $this->user_lottery->userLotteryList($postData, $count, $num);
        }

        echo json_encode(
            array(
                'status' => ($total == 0) ? FALSE : TRUE,
                'html'   => $this->load->view('home/award/lotteryUserList', $data, TRUE),
                'more'   => ($total > ($count+$num)) ? TRUE : FALSE
            )
        );
        exit;
    }
}